package com.ruoyi.project.remake.controller.api;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.remake.domain.TRemake;
import com.ruoyi.project.remake.domain.enmu.RemakeTypeState;
import com.ruoyi.project.remake.service.ITRemakeService;
import com.ruoyi.project.remake.service.RemakeLoginService;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.system.service.ITDoctorUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 质检和高拍程序登录Controller
 *
 * @author ruoyi
 * @date 2020-08-18
 */
@Api("质检和高拍程序登录相关接口")
@RestController
@RequestMapping("/api/remake")
public class RemakeLoginController extends BaseController {
    @Autowired
    private ITRemakeService itRemakeService;
    @Autowired
    private RemakeLoginService remakeLoginService;
    @Autowired
    private ITDoctorUserService tDoctorUserService;

    /**
     * 翻拍端登录
     *
     * @param userName 用户名
     * @param password 密码
     * @param type     类型
     * @return
     */
    @ApiOperation("翻拍端登录")
    @PostMapping("/loginByUseranme")
    public AjaxResult loginByMoblie(String userName, String password, Long type) {

        TRemake remake = itRemakeService.selectByUsername(userName);
        if (remake == null) {
            return AjaxResult.error("不存在该用户名");
        }
        if (type == null) {
            return AjaxResult.error("登录类型选择错误，请联系管理员");
        }
        if (remake.getType().longValue() != type.longValue()) {
            return AjaxResult.error("请检查登录端口是否正确！");
        }
        return login(userName, password);
    }

    /**
     * C端打印程序登录
     *
     * @param userName 用户名
     * @param password
     * @param type     类型
     * @return
     */
    @ApiOperation("C端打印程序登录,type 为1代表打印用户，为0代表医生登录")
    @PostMapping("/loginPrint")
    public AjaxResult loginPrint(String userName, String password, Long type) {
        AjaxResult result = null;
        if (type == null) {
            return AjaxResult.error("参数存在错误");
        }
        if (type.longValue() == 1) {
            result = remakeLoginService.loginPrint(userName, password);
        } else if (type.longValue() == 0) {
            result = tDoctorUserService.loginPrint(userName, password);
        }
        return result;
    }

    /**
     * 统一处理从程序
     */
    public AjaxResult login(String userName, String password) {
        if (StringUtils.isBlank(userName)) {
            return AjaxResult.error("用户名不允许为空");
        }
        if (StringUtils.isBlank(password)) {
            return AjaxResult.error("密码不允许为空");
        }
        try {
            AjaxResult ajaxResult = remakeLoginService.login(userName, password);
            return ajaxResult;
        } catch (Exception e) {
            this.logger.error(e.getMessage(), e);
            return AjaxResult.error(HttpStatus.ERROR, "系统内部异常");
        }
    }
}
